package com.example.administrator.pregnancyassistence.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.pregnancyassistence.R;
import com.example.administrator.pregnancyassistence.activity.ShowNewsContentActivity;
import com.example.administrator.pregnancyassistence.bean.NewsBean;
import com.example.administrator.pregnancyassistence.constants.JumpConstants;

import java.util.ArrayList;
import java.util.List;

public class NewsAdapter extends BaseAdapter {

    private List<NewsBean> newsBeanList = new ArrayList<>();
    private LayoutInflater mInflater;

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NewsBean itemNewsBean = (NewsBean) view.getTag(R.id.item_tag_object);
            Intent intent = new Intent(view.getContext(), ShowNewsContentActivity.class);
            intent.putExtra(JumpConstants.NEWS_TITLE, itemNewsBean.getName());
            view.getContext().startActivity(intent);
        }
    };

    public NewsAdapter(Context context, List<NewsBean> data) {
        newsBeanList = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return newsBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return newsBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        //判断是否有缓存
        if (convertView == null) {
            //通过LayoutInflate实例化布局
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_news_one_image, parent, false);
            viewHolder.iv_article_image = (ImageView) convertView.findViewById(R.id.iv_article_image);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.ll_show_label = (LinearLayout) convertView.findViewById(R.id.ll_show_label);
            convertView.setTag(viewHolder);
        } else {
            //通过tag找到缓存的布局
            viewHolder = (ViewHolder) convertView.getTag();
        }

        NewsBean newsBean = newsBeanList.get(position);
        //为每个控件设置 每条对应的内容对象
        convertView.setTag(R.id.item_tag_object, newsBean);
        convertView.setOnClickListener(onClickListener);
        ImageView iv_article_image = viewHolder.iv_article_image;
        Glide.with(iv_article_image.getContext())
                .load(newsBean.getNewsImage())
                .into(iv_article_image);

        viewHolder.tvTitle.setText(newsBean.getName());
        LinearLayout ll_show_label = viewHolder.ll_show_label;

        //动态添加 标签
        ll_show_label.removeAllViews();
        for (int i = 0; i < newsBean.getTagList().size(); i++) {
            if (i >= 3) {
                continue;
            }
            View tv_label = LayoutInflater.from(convertView.getContext())
                    .inflate(R.layout.item_news_tag, ll_show_label, false);
            if (tv_label instanceof TextView) {
                ViewGroup.LayoutParams layoutParams = tv_label.getLayoutParams();

                if (layoutParams instanceof LinearLayout.LayoutParams) {
                    ((LinearLayout.LayoutParams) layoutParams).leftMargin = 10;
                }
                ((TextView) tv_label).setText(newsBean.getTagList().get(i));

                ll_show_label.addView(tv_label);
            }
        }
        return convertView;
    }


    //使用ViewHolder
    private static class ViewHolder {
        private LinearLayout ll_show_label;
        private TextView tvTitle;
        private ImageView iv_article_image;
    }
}
